﻿package njClass.UI{
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import njClass.bat.*;
	
	public class PlusSlider extends Sprite {
		private var limitNum:int=100;
		private var Value:int;
		private var slider:Sprite=new Sprite();
		private var bg:Sprite=new Sprite;
		private var sider:Sprite=new Sprite();
		private var positions:Array=new Array();
		private var dates:Array=new Array();
		private var Txts:Array=new Array();
		private var txt_ban:TextField=new TextField();
		private var used:Boolean=true;
		
		public function PlusSlider(w:int,h:int,datas:Array,txts:Array,btnW:Number=10,bgObj:*=null,upSider:*=null,overSider:*=null,defaultID:*=-999) {
			dates=datas;
			Txts=txts;
			limitNum=datas.length;
			if(Number(defaultID)==-999){
				defaultID=dates[0];
			}
			Value=defaultID;
			if (bgObj==null) {
				bg.addChild(doDraw(w,h,0x6699FF));
			} else {
				bg.addChild(new bgObj());
				bg.width=w;
				bg.height=h;
			}
			slider.addChild(bg);
			for (var i:int=0; i<datas.length; i++) {
				var pos:SimpleButton=new SimpleButton();
				pos.upState=doDraw(w/limitNum,3*h);
				pos.hitTestState=doDraw(w/limitNum,3*h);
				pos.alpha=0;
				pos.x=i*w/limitNum;
				pos.y=-h;
				if(used){
					pos.addEventListener(MouseEvent.MOUSE_DOWN,JumpToData);
				}
				positions.push(pos);
				slider.addChild(pos);
				doDrawLine(i*(w-btnW)/(limitNum-1)+4,-2,i*(w-btnW)/(limitNum-1)+4,-6);
			}
			if (upSider==null) {
				var siderUp:Sprite=new Sprite();
				siderUp.addChild(doDraw(btnW,h*3,0xCC9900));
				siderUp.y=-h;
				var siderOver:Sprite=new Sprite();
				siderOver.addChild(doDraw(btnW,h*3,0xCC99FF));
				siderOver.y=-h;
				var siderhitTest:Sprite=new Sprite();
				siderhitTest.addChild(doDraw(3*btnW,h*3,0xCC99FF));
				siderhitTest.y=-h;
				siderhitTest.x=-btnW;
			} else {
				siderUp=new upSider();
				siderUp.y=-siderUp.height/3;
				siderUp.width=btnW;
				siderOver=new overSider();
				siderOver.y=-siderUp.height/3;
				siderOver.width=btnW;
				siderhitTest=new upSider();
				siderhitTest.width=3*btnW;
				//siderhitTest.scaleX=3;
				siderhitTest.y=-siderUp.height/3;
				siderhitTest.x=-btnW;
			}
			var siderBtn:SimpleButton=new SimpleButton();
			siderBtn.upState=siderUp;
			siderBtn.hitTestState=siderhitTest;
			siderBtn.downState=siderBtn.overState=siderOver;
			sider.addChild(siderBtn);
			sider.x=((bg.width-sider.width)/(limitNum-1))*datas.indexOf(defaultID);
			slider.addChild(sider);
			txt_ban.x=w+20;
			txt_ban.y=-2;
			txt_ban.selectable=false;
			txt_ban.defaultTextFormat=new TextFormat("宋体",12);
			TextPlus.TextAlpha(txt_ban);
			txt_ban.text=txts[datas.indexOf(defaultID)];
			slider.addChild(txt_ban);
			if(used){
				sider.addEventListener(MouseEvent.MOUSE_DOWN,startListener);
				txt_ban.addEventListener(Event.CHANGE,updateValue);
			}
			//myMove.SliderMove(sider,bg);
			addChild(slider);
		}
		
		public function get enabled():Boolean{
			return used;
		}
		
		public function get text():String{
			return txt_ban.text;
		}
		
		public function set enabled(forUse:Boolean):void{
			sider.removeEventListener(MouseEvent.MOUSE_DOWN,startListener);
			txt_ban.removeEventListener(Event.CHANGE,updateValue);
			for(var i:int=0;i<positions.length;i++){
				positions[i].removeEventListener(MouseEvent.MOUSE_DOWN,JumpToData);
			}
			used=forUse;
			if(used){
				slider.alpha=1;
				sider.addEventListener(MouseEvent.MOUSE_DOWN,startListener);
				txt_ban.addEventListener(Event.CHANGE,updateValue);
				for(i=0;i<positions.length;i++){
					positions[i].addEventListener(MouseEvent.MOUSE_DOWN,JumpToData);
				}
			}else{
				slider.alpha=0.5;
			}
		}
		
		private function JumpToData(e:MouseEvent):void {
			MovePlus.ChangeSpeedMove(sider,sider.x,((bg.width-sider.width)/(limitNum-1))*positions.indexOf(e.target),8);
			updateValue(((bg.width-sider.width)/(limitNum-1))*positions.indexOf(e.target));
		}
		
		private function startListener(e:MouseEvent):void {
			slider.stage.addEventListener(MouseEvent.MOUSE_MOVE,SilderScroll);
			sider.stage.addEventListener(MouseEvent.MOUSE_UP,stopListener);
		}
		
		private function stopListener(e:MouseEvent):void {
			slider.stage.removeEventListener(MouseEvent.MOUSE_MOVE,SilderScroll);
			sider.stage.removeEventListener(MouseEvent.MOUSE_UP,stopListener);
		}
		
		private function SilderScroll(e:MouseEvent):void {
			var checkPosition:Boolean;
			var tempX:Number=sider.x;
			for (var i:int=0; i<positions.length; i++) {
				if (i!=positions.length-1) {
					checkPosition=(slider.mouseX>=positions[i].x&&slider.mouseX<=positions[i+1].x);
				} else {
					checkPosition=(slider.mouseX>=positions[i].x);
				}
				if (checkPosition) {
					sider.x=((bg.width-sider.width)/(limitNum-1))*i;
					if (sider.x!=tempX) {
						updateValue(sider.x);
					}
					return;
				}
			}
		}
		
		private function updateValue(num:Number):void {
			txt_ban.text=Txts[Math.round(num/((bg.width-sider.width)/(limitNum-1)))];
			Value=dates[Math.round(num/((bg.width-sider.width)/(limitNum-1)))];
			dispatchEvent(new Event("SliderChange"));
		}
		
		public function get value():int {
			return Value;
		}
		
		public function set value(num:int):void {
			Value=num;
			txt_ban.text=Txts[dates.indexOf(num)];
			MovePlus.ChangeSpeedMove(sider,sider.x,((bg.width-sider.width)/(limitNum-1))*dates.indexOf(num),8);
			//sider.x=((bg.width+positions[0].width)/limitNum)*dates.indexOf(num);
		}
		
		private function doDraw(w:Number,h:Number,bgColor:uint=0x000000):Shape {
			var child:Shape=new Shape;
			child.graphics.beginFill(bgColor);
			child.graphics.drawRoundRect(0,0,w,h,5);
			child.graphics.endFill();
			return child;
		}
		
		private function doDrawLine(X0:Number,Y0:Number,X:Number,Y:Number,bgColor:uint=0x000000):void {
			var temp:Shape=new Shape();
			temp.graphics.lineStyle(1,bgColor);
			temp.graphics.moveTo(X0,Y0);
			temp.graphics.lineTo(X,Y);
			slider.addChild(temp);
		}
	}
}